﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.events.*;
    import morn.core.handlers.*;

    public class RadioGroup extends Box implements IItem, ISkinAbleContainer
    {
        protected var _items:Vector.<RadioButton>;
        protected var _selectHandler:Handler;
        protected var _selectedIndex:int = -1;
        protected var _skin:String;
        protected var _labels:String;
        protected var _labelColors:String;
        protected var _labelStroke:String;
        protected var _labelSize:Object;
        protected var _labelBold:Object;
        protected var _labelMargin:String;
        protected var _direction:String = "horizontal";
        private var _skinChanged:Boolean = false;
        public static const HORIZENTAL:String = "horizontal";
        public static const VERTICAL:String = "vertical";

        public function RadioGroup(param1:String = null, param2:String = null)
        {
            this.skin = param2;
            this.labels = param1;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.skin = param1 as String;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        public function setItems(param1:Array) : void
        {
            var _loc_5:RadioButton = null;
            removeAllChild();
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = param1.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_5 = param1[_loc_3];
                if (_loc_5)
                {
                    _loc_5.name = "item" + _loc_2;
                    addChild(_loc_5);
                    _loc_2++;
                }
                _loc_3++;
            }
            this.initItems();
            return;
        }// end function

        public function addItem(param1:RadioButton) : void
        {
            param1.name = "item" + this._items.length;
            addChild(param1);
            this.initItems();
            return;
        }// end function

        public function initItems() : void
        {
            var _loc_2:RadioButton = null;
            this._items = new Vector.<RadioButton>;
            var _loc_1:int = 0;
            while (_loc_1 < int.MAX_VALUE)
            {
                
                _loc_2 = getChildByName("item" + _loc_1) as RadioButton;
                if (_loc_2 == null)
                {
                    break;
                }
                this._items.push(_loc_2);
                _loc_2.selected = _loc_1 == this._selectedIndex;
                _loc_2.clickHandler = new Handler(this.itemClick, [_loc_1]);
                _loc_1++;
            }
            return;
        }// end function

        protected function itemClick(param1:int) : void
        {
            this.selectedIndex = param1;
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._selectedIndex;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            if (this._selectedIndex != param1)
            {
                this.setSelect(this._selectedIndex, false);
                this._selectedIndex = param1;
                this.setSelect(this._selectedIndex, true);
                sendEvent(Event.SELECT);
                if (this._selectHandler != null)
                {
                    this._selectHandler.executeWith([this._selectedIndex]);
                }
            }
            return;
        }// end function

        protected function setSelect(param1:int, param2:Boolean) : void
        {
            if (this._items && param1 > -1 && param1 < this._items.length)
            {
                this._items[param1].selected = param2;
            }
            return;
        }// end function

        public function get selectHandler() : Handler
        {
            return this._selectHandler;
        }// end function

        public function set selectHandler(param1:Handler) : void
        {
            this._selectHandler = param1;
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._skin != param1)
            {
                this._skin = param1;
                callLater(this.changeLabels);
            }
            this._skinChanged = true;
            return;
        }// end function

        public function get labels() : String
        {
            return this._labels;
        }// end function

        public function set labels(param1:String) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:RadioButton = null;
            if (this._labels != param1)
            {
                this._labels = param1;
                removeAllChild();
                callLater(this.changeLabels);
                if (Boolean(this._labels))
                {
                    _loc_2 = this._labels.split(",");
                    _loc_3 = 0;
                    _loc_4 = _loc_2.length;
                    while (_loc_3 < _loc_4)
                    {
                        
                        _loc_5 = new RadioButton(this._skin, _loc_2[_loc_3]);
                        _loc_5.name = "item" + _loc_3;
                        addChild(_loc_5);
                        _loc_3++;
                    }
                }
                this.initItems();
            }
            return;
        }// end function

        public function get labelColors() : String
        {
            return this._labelColors;
        }// end function

        public function set labelColors(param1:String) : void
        {
            if (this._labelColors != param1)
            {
                this._labelColors = param1;
                callLater(this.changeLabels);
            }
            return;
        }// end function

        public function get labelStroke() : String
        {
            return this._labelStroke;
        }// end function

        public function set labelStroke(param1:String) : void
        {
            if (this._labelStroke != param1)
            {
                this._labelStroke = param1;
                callLater(this.changeLabels);
            }
            return;
        }// end function

        public function get labelSize() : Object
        {
            return this._labelSize;
        }// end function

        public function set labelSize(param1:Object) : void
        {
            if (this._labelSize != param1)
            {
                this._labelSize = param1;
                callLater(this.changeLabels);
            }
            return;
        }// end function

        public function get labelBold() : Object
        {
            return this._labelBold;
        }// end function

        public function set labelBold(param1:Object) : void
        {
            if (this._labelBold != param1)
            {
                this._labelBold = param1;
                callLater(this.changeLabels);
            }
            return;
        }// end function

        public function get labelMargin() : String
        {
            return this._labelMargin;
        }// end function

        public function set labelMargin(param1:String) : void
        {
            if (this._labelMargin != param1)
            {
                this._labelMargin = param1;
                callLater(this.changeLabels);
            }
            return;
        }// end function

        protected function changeLabels() : void
        {
            var _loc_4:RadioButton = null;
            var _loc_1:Number = 0;
            var _loc_2:int = 0;
            var _loc_3:* = this._items.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = this._items[_loc_2];
                if (this._skin)
                {
                    _loc_4.skin = this._skin;
                }
                if (this._labelColors)
                {
                    _loc_4.labelColors = this._labelColors;
                }
                if (this._labelStroke)
                {
                    _loc_4.labelStroke = this._labelStroke;
                }
                if (this._labelSize)
                {
                    _loc_4.labelSize = this._labelSize;
                }
                if (this._labelBold)
                {
                    _loc_4.labelBold = this._labelBold;
                }
                if (this._labelMargin)
                {
                    _loc_4.labelMargin = this._labelMargin;
                }
                if (this._direction == HORIZENTAL)
                {
                    _loc_4.y = 0;
                    _loc_4.x = _loc_1;
                    _loc_1 = _loc_1 + _loc_4.width;
                }
                else
                {
                    _loc_4.x = 0;
                    _loc_4.y = _loc_1;
                    _loc_1 = _loc_1 + _loc_4.height;
                }
                _loc_2++;
            }
            return;
        }// end function

        override public function commitMeasure() : void
        {
            exeCallLater(this.changeLabels);
            return;
        }// end function

        public function get items() : Vector.<RadioButton>
        {
            return this._items;
        }// end function

        public function get selection() : RadioButton
        {
            return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? (this._items[this._selectedIndex]) : (null);
        }// end function

        public function set selection(param1:RadioButton) : void
        {
            this.selectedIndex = this._items.indexOf(param1);
            return;
        }// end function

        public function get selectedValue() : Object
        {
            return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? (this._items[this._selectedIndex].value) : (null);
        }// end function

        public function set selectedValue(param1:Object) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:RadioButton = null;
            if (this._items)
            {
                _loc_2 = 0;
                _loc_3 = this._items.length;
                while (_loc_2 < _loc_3)
                {
                    
                    _loc_4 = this._items[_loc_2];
                    if (_loc_4.value == param1)
                    {
                        this.selectedIndex = _loc_2;
                        break;
                    }
                    _loc_2++;
                }
            }
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            var _loc_2:String = null;
            _dataSource = param1;
            if (param1 is int || param1 is String)
            {
                this.selectedIndex = int(param1);
            }
            else if (param1 is Array)
            {
                this.labels = (param1 as Array).join(",");
            }
            else
            {
                for (_loc_2 in _dataSource)
                {
                    
                    if (hasOwnProperty(_loc_2))
                    {
                        this[_loc_2] = _dataSource[_loc_2];
                    }
                }
            }
            return;
        }// end function

        public function get direction() : String
        {
            return this._direction;
        }// end function

        public function set direction(param1:String) : void
        {
            this._direction = param1;
            callLater(this.changeLabels);
            return;
        }// end function

    }
}
